#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "customtabstyle.h"
#include <QtGui>
#include <QMessageBox>
#include <QLineEdit>
#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    int index = 0;
    ui->setupUi(this);
    ui->tabWidget->tabBar()->setStyle(new CustomTabStyle);
    setWindowTitle("Echo Plugin Example");

    if (!pluginManager.loadPlugin(qApp->applicationDirPath()))
    {
        QMessageBox::information(this, "Error", "Could not load the plugin");
    }

    // 添加设置程序界面，并将设置界面添加到Tab表中
    settingFrom = SettingForm::instances();
    ui->tabWidget->addTab(settingFrom, "配置界面");

    for(int i = 0; i < PLUGIN_MAXCOUNT; i++)
    {
        if( pluginManager.pluginInterfaceT1V0[i] != nullptr )
        {
            settingFrom->AddLibInfo(index, pluginManager.pluginInterfaceT1V0[i]->getPluginInfo());
            ui->tabWidget->addTab(pluginManager.pluginInterfaceT1V0[i]->getWidget(),pluginManager.pluginInterfaceT1V0[i]->getPluginInfo()->name);
            index++;
        }
        else
        {
            break;
        }
    }

    for(int i = 0; i < PLUGIN_MAXCOUNT; i++)
    {
        if( pluginManager.pluginInterfaceT2V0[i] != nullptr )
        {
            settingFrom->AddLibInfo(index, pluginManager.pluginInterfaceT2V0[i]->getPluginInfo());
            ui->tabWidget->addTab(pluginManager.pluginInterfaceT2V0[i]->getWidget(),pluginManager.pluginInterfaceT2V0[i]->getPluginInfo()->name);
            index++;
        }
        else
        {
            break;
        }
    }
    ui->statusBar->showMessage("加载 插件完成");
}

MainWindow::~MainWindow()
{
    delete ui;
    if (!pluginManager.unloadPlugin())
    {
        QMessageBox::information(this, "Error", "Could not unload the plugin");
    }
}
